using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Shared.Domain.Photos.Batch
{
    /// <summary>
    /// 照片批量操作实体
    /// 支持各种批量处理任务的调度和执行
    /// </summary>
    public class PhotoBatchOperation : FullAuditedAggregateRoot<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; protected set; }

        /// <summary>
        /// 操作名称
        /// </summary>
        [Required]
        [MaxLength(200)]
        public virtual string Name { get; protected set; }

        /// <summary>
        /// 操作描述
        /// </summary>
        [MaxLength(1000)]
        public virtual string Description { get; protected set; }

        /// <summary>
        /// 操作类型
        /// </summary>
        public virtual BatchOperationType OperationType { get; protected set; }

        /// <summary>
        /// 操作状态
        /// </summary>
        public virtual BatchOperationStatus Status { get; protected set; }

        /// <summary>
        /// 优先级
        /// </summary>
        public virtual int Priority { get; protected set; }

        /// <summary>
        /// 操作参数
        /// </summary>
        public virtual Dictionary<string, object> Parameters { get; protected set; }

        /// <summary>
        /// 照片ID列表
        /// </summary>
        public virtual List<Guid> PhotoIds { get; protected set; }

        /// <summary>
        /// 照片筛选条件
        /// </summary>
        public virtual PhotoSelectionCriteria SelectionCriteria { get; protected set; }

        /// <summary>
        /// 操作发起者ID
        /// </summary>
        public virtual Guid? InitiatorId { get; protected set; }

        /// <summary>
        /// 操作发起者名称
        /// </summary>
        [MaxLength(200)]
        public virtual string InitiatorName { get; protected set; }

        /// <summary>
        /// 计划执行时间
        /// </summary>
        public virtual DateTime? ScheduledTime { get; protected set; }

        /// <summary>
        /// 开始执行时间
        /// </summary>
        public virtual DateTime? StartTime { get; protected set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public virtual DateTime? CompletionTime { get; protected set; }

        /// <summary>
        /// 超时时间（分钟）
        /// </summary>
        public virtual int TimeoutMinutes { get; protected set; }

        /// <summary>
        /// 最大重试次数
        /// </summary>
        public virtual int MaxRetryCount { get; protected set; }

        /// <summary>
        /// 当前重试次数
        /// </summary>
        public virtual int CurrentRetryCount { get; protected set; }

        /// <summary>
        /// 批量大小
        /// </summary>
        public virtual int BatchSize { get; protected set; }

        /// <summary>
        /// 是否允许部分成功
        /// </summary>
        public virtual bool AllowPartialSuccess { get; protected set; }

        /// <summary>
        /// 是否发送通知
        /// </summary>
        public virtual bool SendNotification { get; protected set; }

        /// <summary>
        /// 通知邮箱列表
        /// </summary>
        public virtual List<string> NotificationEmails { get; protected set; }

        /// <summary>
        /// 操作结果
        /// </summary>
        public virtual BatchOperationResult Result { get; protected set; }

        /// <summary>
        /// 操作进度
        /// </summary>
        public virtual BatchOperationProgress Progress { get; protected set; }

        /// <summary>
        /// 操作配置
        /// </summary>
        public virtual BatchOperationConfiguration Configuration { get; protected set; }

        /// <summary>
        /// 操作日志
        /// </summary>
        public virtual List<BatchOperationLog> Logs { get; protected set; }

        /// <summary>
        /// 子操作集合
        /// </summary>
        public virtual List<PhotoBatchSubOperation> SubOperations { get; protected set; }

        /// <summary>
        /// 操作依赖项
        /// </summary>
        public virtual List<Guid> DependencyOperationIds { get; protected set; }

        protected PhotoBatchOperation()
        {
            Parameters = new Dictionary<string, object>();
            PhotoIds = new List<Guid>();
            SelectionCriteria = new PhotoSelectionCriteria();
            Result = new BatchOperationResult();
            Progress = new BatchOperationProgress();
            Configuration = new BatchOperationConfiguration();
            Logs = new List<BatchOperationLog>();
            SubOperations = new List<PhotoBatchSubOperation>();
            NotificationEmails = new List<string>();
            DependencyOperationIds = new List<Guid>();
        }

        public PhotoBatchOperation(
            Guid id,
            string name,
            BatchOperationType operationType,
            List<Guid> photoIds = null,
            PhotoSelectionCriteria selectionCriteria = null,
            Guid? initiatorId = null,
            string initiatorName = null,
            Guid? tenantId = null) : base(id)
        {
            Name = name;
            OperationType = operationType;
            InitiatorId = initiatorId;
            InitiatorName = initiatorName;
            TenantId = tenantId;
            Status = BatchOperationStatus.Pending;
            Priority = 0;
            TimeoutMinutes = 60;
            MaxRetryCount = 3;
            BatchSize = 100;
            AllowPartialSuccess = true;
            SendNotification = true;

            Parameters = new Dictionary<string, object>();
            PhotoIds = photoIds ?? new List<Guid>();
            SelectionCriteria = selectionCriteria ?? new PhotoSelectionCriteria();
            Result = new BatchOperationResult();
            Progress = new BatchOperationProgress();
            Configuration = new BatchOperationConfiguration();
            Logs = new List<BatchOperationLog>();
            SubOperations = new List<PhotoBatchSubOperation>();
            NotificationEmails = new List<string>();
            DependencyOperationIds = new List<Guid>();
        }

        /// <summary>
        /// 开始执行操作
        /// </summary>
        public virtual void StartExecution()
        {
            if (Status != BatchOperationStatus.Pending && Status != BatchOperationStatus.Retrying)
                throw new InvalidOperationException($"无法在状态 {Status} 下开始执行操作");

            Status = BatchOperationStatus.Running;
            StartTime = DateTime.Now;
            Progress.StartTime = StartTime;
            AddLog(BatchLogLevel.Info, "操作开始执行", $"操作类型: {OperationType}, 照片数量: {GetTotalPhotoCount()}");
        }

        /// <summary>
        /// 完成操作
        /// </summary>
        public virtual void CompleteOperation(bool success, string errorMessage = null)
        {
            CompletionTime = DateTime.Now;
            Progress.EndTime = CompletionTime;
            Progress.Duration = Progress.CalculateDuration();

            if (success)
            {
                Status = BatchOperationStatus.Completed;
                Result.IsSuccess = true;
                AddLog(BatchLogLevel.Info, "操作完成", $"成功处理 {Result.SuccessCount} 个项目");
            }
            else
            {
                Status = BatchOperationStatus.Failed;
                Result.IsSuccess = false;
                Result.ErrorMessage = errorMessage;
                AddLog(BatchLogLevel.Error, "操作失败", errorMessage);
            }
        }

        /// <summary>
        /// 取消操作
        /// </summary>
        public virtual void CancelOperation(string reason = null)
        {
            Status = BatchOperationStatus.Cancelled;
            CompletionTime = DateTime.Now;
            Progress.EndTime = CompletionTime;
            Result.CancelReason = reason;
            AddLog(BatchLogLevel.Warning, "操作已取消", reason);
        }

        /// <summary>
        /// 暂停操作
        /// </summary>
        public virtual void PauseOperation(string reason = null)
        {
            if (Status != BatchOperationStatus.Running)
                throw new InvalidOperationException($"无法在状态 {Status} 下暂停操作");

            Status = BatchOperationStatus.Paused;
            AddLog(BatchLogLevel.Info, "操作已暂停", reason);
        }

        /// <summary>
        /// 恢复操作
        /// </summary>
        public virtual void ResumeOperation()
        {
            if (Status != BatchOperationStatus.Paused)
                throw new InvalidOperationException($"无法在状态 {Status} 下恢复操作");

            Status = BatchOperationStatus.Running;
            AddLog(BatchLogLevel.Info, "操作已恢复");
        }

        /// <summary>
        /// 重试操作
        /// </summary>
        public virtual void RetryOperation()
        {
            if (CurrentRetryCount >= MaxRetryCount)
            {
                CompleteOperation(false, "已达到最大重试次数");
                return;
            }

            CurrentRetryCount++;
            Status = BatchOperationStatus.Retrying;
            StartTime = DateTime.Now;
            Result.Reset();
            Progress.Reset();
            AddLog(BatchLogLevel.Info, "开始重试操作", $"重试次数: {CurrentRetryCount}/{MaxRetryCount}");
        }

        /// <summary>
        /// 更新进度
        /// </summary>
        public virtual void UpdateProgress(int processedCount, int successCount, int failureCount)
        {
            Progress.ProcessedCount = processedCount;
            Result.SuccessCount = successCount;
            Result.FailureCount = failureCount;
            Progress.UpdatePercentage();

            if (processedCount >= GetTotalPhotoCount())
            {
                CompleteOperation(true);
            }
        }

        /// <summary>
        /// 添加处理结果
        /// </summary>
        public virtual void AddProcessResult(Guid photoId, bool success, string errorMessage = null)
        {
            var result = new PhotoProcessResult
            {
                PhotoId = photoId,
                Success = success,
                ErrorMessage = errorMessage,
                ProcessTime = DateTime.Now
            };

            Result.ProcessResults.Add(result);

            if (success)
            {
                Result.SuccessCount++;
            }
            else
            {
                Result.FailureCount++;
                Result.FailureDetails[photoId] = errorMessage;
            }
        }

        /// <summary>
        /// 添加操作日志
        /// </summary>
        public virtual void AddLog(BatchLogLevel level, string message, string details = null)
        {
            var log = new BatchOperationLog
            {
                Level = level,
                Message = message,
                Details = details,
                LogTime = DateTime.Now
            };

            Logs.Add(log);
        }

        /// <summary>
        /// 更新操作参数
        /// </summary>
        public virtual void UpdateParameters(Dictionary<string, object> parameters)
        {
            Parameters = parameters ?? new Dictionary<string, object>();
        }

        /// <summary>
        /// 设置操作参数
        /// </summary>
        public virtual void SetParameter(string key, object value)
        {
            Parameters[key] = value;
        }

        /// <summary>
        /// 添加照片ID
        /// </summary>
        public virtual void AddPhotoId(Guid photoId)
        {
            if (!PhotoIds.Contains(photoId))
            {
                PhotoIds.Add(photoId);
            }
        }

        /// <summary>
        /// 移除照片ID
        /// </summary>
        public virtual void RemovePhotoId(Guid photoId)
        {
            PhotoIds.Remove(photoId);
        }

        /// <summary>
        /// 设置照片ID列表
        /// </summary>
        public virtual void SetPhotoIds(List<Guid> photoIds)
        {
            PhotoIds = photoIds ?? new List<Guid>();
        }

        /// <summary>
        /// 更新筛选条件
        /// </summary>
        public virtual void UpdateSelectionCriteria(PhotoSelectionCriteria criteria)
        {
            SelectionCriteria = criteria ?? new PhotoSelectionCriteria();
        }

        /// <summary>
        /// 添加通知邮箱
        /// </summary>
        public virtual void AddNotificationEmail(string email)
        {
            if (!string.IsNullOrWhiteSpace(email) && !NotificationEmails.Contains(email))
            {
                NotificationEmails.Add(email);
            }
        }

        /// <summary>
        /// 添加依赖操作
        /// </summary>
        public virtual void AddDependencyOperation(Guid operationId)
        {
            if (!DependencyOperationIds.Contains(operationId))
            {
                DependencyOperationIds.Add(operationId);
            }
        }

        /// <summary>
        /// 获取总照片数量
        /// </summary>
        public virtual int GetTotalPhotoCount()
        {
            if (PhotoIds.Count > 0)
                return PhotoIds.Count;

            // 如果没有指定照片ID，则需要根据筛选条件计算
            // 这里返回0，实际计算在服务层进行
            return 0;
        }

        /// <summary>
        /// 检查是否可以执行
        /// </summary>
        public virtual bool CanExecute()
        {
            return Status == BatchOperationStatus.Pending ||
                   Status == BatchOperationStatus.Retrying;
        }

        /// <summary>
        /// 检查是否已完成
        /// </summary>
        public virtual bool IsCompleted()
        {
            return Status == BatchOperationStatus.Completed ||
                   Status == BatchOperationStatus.Failed ||
                   Status == BatchOperationStatus.Cancelled;
        }

        /// <summary>
        /// 检查是否超时
        /// </summary>
        public virtual bool IsTimeout()
        {
            if (!StartTime.HasValue || TimeoutMinutes <= 0)
                return false;

            return DateTime.Now > StartTime.Value.AddMinutes(TimeoutMinutes);
        }

        /// <summary>
        /// 获取执行耗时
        /// </summary>
        public virtual TimeSpan? GetExecutionDuration()
        {
            if (!StartTime.HasValue)
                return null;

            var endTime = CompletionTime ?? DateTime.Now;
            return endTime - StartTime.Value;
        }
    }

    /// <summary>
    /// 批量操作类型枚举
    /// </summary>
    public enum BatchOperationType
    {
        /// <summary>
        /// 移动照片
        /// </summary>
        MovePhotos = 0,

        /// <summary>
        /// 复制照片
        /// </summary>
        CopyPhotos = 1,

        /// <summary>
        /// 删除照片
        /// </summary>
        DeletePhotos = 2,

        /// <summary>
        /// 添加标签
        /// </summary>
        AddTags = 3,

        /// <summary>
        /// 移除标签
        /// </summary>
        RemoveTags = 4,

        /// <summary>
        /// 重命名照片
        /// </summary>
        RenamePhotos = 5,

        /// <summary>
        /// 旋转照片
        /// </summary>
        RotatePhotos = 6,

        /// <summary>
        /// 调整大小
        /// </summary>
        ResizePhotos = 7,

        /// <summary>
        /// 压缩照片
        /// </summary>
        CompressPhotos = 8,

        /// <summary>
        /// 格式转换
        /// </summary>
        ConvertFormat = 9,

        /// <summary>
        /// 添加水印
        /// </summary>
        AddWatermark = 10,

        /// <summary>
        /// 提取元数据
        /// </summary>
        ExtractMetadata = 11,

        /// <summary>
        /// 人脸识别
        /// </summary>
        FaceRecognition = 12,

        /// <summary>
        /// 场景识别
        /// </summary>
        SceneRecognition = 13,

        /// <summary>
        /// 物体识别
        /// </summary>
        ObjectRecognition = 14,

        /// <summary>
        /// 文字识别
        /// </summary>
        TextRecognition = 15,

        /// <summary>
        /// 生成缩略图
        /// </summary>
        GenerateThumbnails = 16,

        /// <summary>
        /// 备份照片
        /// </summary>
        BackupPhotos = 17,

        /// <summary>
        /// 恢复照片
        /// </summary>
        RestorePhotos = 18,

        /// <summary>
        /// 导出照片
        /// </summary>
        ExportPhotos = 19,

        /// <summary>
        /// 导入照片
        /// </summary>
        ImportPhotos = 20,

        /// <summary>
        /// 同步照片
        /// </summary>
        SyncPhotos = 21,

        /// <summary>
        /// 验证完整性
        /// </summary>
        VerifyIntegrity = 22,

        /// <summary>
        /// 清理重复
        /// </summary>
        CleanupDuplicates = 23,

        /// <summary>
        /// 自定义操作
        /// </summary>
        CustomOperation = 24
    }

    /// <summary>
    /// 批量操作状态枚举
    /// </summary>
    public enum BatchOperationStatus
    {
        /// <summary>
        /// 等待中
        /// </summary>
        Pending = 0,

        /// <summary>
        /// 运行中
        /// </summary>
        Running = 1,

        /// <summary>
        /// 已暂停
        /// </summary>
        Paused = 2,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed = 3,

        /// <summary>
        /// 失败
        /// </summary>
        Failed = 4,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled = 5,

        /// <summary>
        /// 重试中
        /// </summary>
        Retrying = 6,

        /// <summary>
        /// 超时
        /// </summary>
        Timeout = 7
    }

    /// <summary>
    /// 照片筛选条件
    /// </summary>
    public class PhotoSelectionCriteria
    {
        /// <summary>
        /// 相册ID列表
        /// </summary>
        public List<Guid> GalleryIds { get; set; } = new();

        /// <summary>
        /// 标签列表
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 排除标签列表
        /// </summary>
        public List<string> ExcludeTags { get; set; } = new();

        /// <summary>
        /// 文件类型列表
        /// </summary>
        public List<string> FileTypes { get; set; } = new();

        /// <summary>
        /// 最小文件大小（字节）
        /// </summary>
        public long? MinFileSize { get; set; }

        /// <summary>
        /// 最大文件大小（字节）
        /// </summary>
        public long? MaxFileSize { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 创建者ID列表
        /// </summary>
        public List<Guid> CreatorIds { get; set; } = new();

        /// <summary>
        /// 地理位置筛选
        /// </summary>
        public GeographicFilter GeographicFilter { get; set; }

        /// <summary>
        /// 是否包含已删除的照片
        /// </summary>
        public bool IncludeDeleted { get; set; } = false;

        /// <summary>
        /// 是否只包含收藏的照片
        /// </summary>
        public bool OnlyFavorites { get; set; } = false;

        /// <summary>
        /// 自定义筛选条件
        /// </summary>
        public Dictionary<string, object> CustomFilters { get; set; } = new();
    }

    /// <summary>
    /// 地理位置筛选
    /// </summary>
    public class GeographicFilter
    {
        /// <summary>
        /// 国家
        /// </summary>
        public string Country { get; set; }

        /// <summary>
        /// 省份
        /// </summary>
        public string Province { get; set; }

        /// <summary>
        /// 城市
        /// </summary>
        public string City { get; set; }

        /// <summary>
        /// 边界框
        /// </summary>
        public GeographicBoundingBox BoundingBox { get; set; }

        /// <summary>
        /// 半径范围（米）
        /// </summary>
        public double? RadiusMeters { get; set; }

        /// <summary>
        /// 中心点纬度
        /// </summary>
        public double? CenterLatitude { get; set; }

        /// <summary>
        /// 中心点经度
        /// </summary>
        public double? CenterLongitude { get; set; }
    }

    /// <summary>
    /// 地理边界框
    /// </summary>
    public class GeographicBoundingBox
    {
        /// <summary>
        /// 最小纬度
        /// </summary>
        public double MinLatitude { get; set; }

        /// <summary>
        /// 最大纬度
        /// </summary>
        public double MaxLatitude { get; set; }

        /// <summary>
        /// 最小经度
        /// </summary>
        public double MinLongitude { get; set; }

        /// <summary>
        /// 最大经度
        /// </summary>
        public double MaxLongitude { get; set; }
    }

    /// <summary>
    /// 批量操作结果
    /// </summary>
    public class BatchOperationResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 跳过数量
        /// </summary>
        public int SkippedCount { get; set; }

        /// <summary>
        /// 总数量
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 取消原因
        /// </summary>
        public string CancelReason { get; set; }

        /// <summary>
        /// 失败详情
        /// </summary>
        public Dictionary<Guid, string> FailureDetails { get; set; } = new();

        /// <summary>
        /// 处理结果列表
        /// </summary>
        public List<PhotoProcessResult> ProcessResults { get; set; } = new();

        /// <summary>
        /// 输出文件路径（如导出操作）
        /// </summary>
        public List<string> OutputPaths { get; set; } = new();

        /// <summary>
        /// 统计信息
        /// </summary>
        public Dictionary<string, object> Statistics { get; set; } = new();

        /// <summary>
        /// 重置结果
        /// </summary>
        public void Reset()
        {
            IsSuccess = false;
            SuccessCount = 0;
            FailureCount = 0;
            SkippedCount = 0;
            ErrorMessage = null;
            CancelReason = null;
            FailureDetails.Clear();
            ProcessResults.Clear();
            OutputPaths.Clear();
            Statistics.Clear();
        }

        /// <summary>
        /// 获取成功率
        /// </summary>
        public double GetSuccessRate()
        {
            return TotalCount > 0 ? (double)SuccessCount / TotalCount : 0;
        }
    }

    /// <summary>
    /// 照片处理结果
    /// </summary>
    public class PhotoProcessResult
    {
        /// <summary>
        /// 照片ID
        /// </summary>
        public Guid PhotoId { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 处理时间
        /// </summary>
        public DateTime ProcessTime { get; set; }

        /// <summary>
        /// 处理耗时（毫秒）
        /// </summary>
        public long Duration { get; set; }

        /// <summary>
        /// 处理结果数据
        /// </summary>
        public Dictionary<string, object> ResultData { get; set; } = new();

        /// <summary>
        /// 警告信息
        /// </summary>
        public List<string> Warnings { get; set; } = new();
    }

    /// <summary>
    /// 批量操作进度
    /// </summary>
    public class BatchOperationProgress
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 总数量
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 已处理数量
        /// </summary>
        public int ProcessedCount { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 完成百分比
        /// </summary>
        public double Percentage { get; set; }

        /// <summary>
        /// 预估剩余时间（秒）
        /// </summary>
        public double? EstimatedRemainingSeconds { get; set; }

        /// <summary>
        /// 当前处理速度（每分钟）
        /// </summary>
        public double CurrentSpeed { get; set; }

        /// <summary>
        /// 平均处理速度（每分钟）
        /// </summary>
        public double AverageSpeed { get; set; }

        /// <summary>
        /// 执行耗时
        /// </summary>
        public TimeSpan? Duration { get; set; }

        /// <summary>
        /// 更新百分比
        /// </summary>
        public void UpdatePercentage()
        {
            if (TotalCount > 0)
            {
                Percentage = (double)ProcessedCount / TotalCount * 100;
            }
        }

        /// <summary>
        /// 计算执行耗时
        /// </summary>
        public TimeSpan CalculateDuration()
        {
            if (!StartTime.HasValue)
                return TimeSpan.Zero;

            var endTime = EndTime ?? DateTime.Now;
            return endTime - StartTime.Value;
        }

        /// <summary>
        /// 重置进度
        /// </summary>
        public void Reset()
        {
            StartTime = null;
            EndTime = null;
            TotalCount = 0;
            ProcessedCount = 0;
            SuccessCount = 0;
            FailureCount = 0;
            Percentage = 0;
            EstimatedRemainingSeconds = null;
            CurrentSpeed = 0;
            AverageSpeed = 0;
            Duration = null;
        }
    }

    /// <summary>
    /// 批量操作配置
    /// </summary>
    public class BatchOperationConfiguration
    {
        /// <summary>
        /// 并发处理数量
        /// </summary>
        public int ConcurrencyCount { get; set; } = 1;

        /// <summary>
        /// 操作间隔（毫秒）
        /// </summary>
        public int OperationIntervalMs { get; set; } = 0;

        /// <summary>
        /// 是否启用断点续传
        /// </summary>
        public bool EnableResume { get; set; } = true;

        /// <summary>
        /// 是否跳过错误
        /// </summary>
        public bool SkipErrors { get; set; } = false;

        /// <summary>
        /// 是否创建备份
        /// </summary>
        public bool CreateBackup { get; set; } = false;

        /// <summary>
        /// 备份路径
        /// </summary>
        public string BackupPath { get; set; }

        /// <summary>
        /// 是否记录详细日志
        /// </summary>
        public bool EnableDetailedLogging { get; set; } = true;

        /// <summary>
        /// 最大日志条数
        /// </summary>
        public int MaxLogEntries { get; set; } = 10000;

        /// <summary>
        /// 是否启用进度报告
        /// </summary>
        public bool EnableProgressReporting { get; set; } = true;

        /// <summary>
        /// 进度报告间隔（秒）
        /// </summary>
        public int ProgressReportIntervalSeconds { get; set; } = 10;

        /// <summary>
        /// 资源限制配置
        /// </summary>
        public ResourceLimits ResourceLimits { get; set; } = new();

        /// <summary>
        /// 通知配置
        /// </summary>
        public NotificationConfig NotificationConfig { get; set; } = new();
    }

    /// <summary>
    /// 资源限制
    /// </summary>
    public class ResourceLimits
    {
        /// <summary>
        /// 最大内存使用量（MB）
        /// </summary>
        public long MaxMemoryMB { get; set; } = 1024;

        /// <summary>
        /// 最大CPU使用率（百分比）
        /// </summary>
        public double MaxCpuUsage { get; set; } = 80;

        /// <summary>
        /// 最大磁盘IO（MB/s）
        /// </summary>
        public double MaxDiskIOMBps { get; set; } = 100;

        /// <summary>
        /// 最大网络带宽（MB/s）
        /// </summary>
        public double MaxNetworkBandwidthMBps { get; set; } = 50;
    }

    /// <summary>
    /// 通知配置
    /// </summary>
    public class NotificationConfig
    {
        /// <summary>
        /// 开始时通知
        /// </summary>
        public bool NotifyOnStart { get; set; } = false;

        /// <summary>
        /// 完成时通知
        /// </summary>
        public bool NotifyOnCompletion { get; set; } = true;

        /// <summary>
        /// 失败时通知
        /// </summary>
        public bool NotifyOnFailure { get; set; } = true;

        /// <summary>
        /// 达到里程碑时通知
        /// </summary>
        public bool NotifyOnMilestone { get; set; } = false;

        /// <summary>
        /// 里程碑百分比
        /// </summary>
        public List<int> MilestonePercentages { get; set; } = new() { 25, 50, 75 };

        /// <summary>
        /// 通知模板
        /// </summary>
        public Dictionary<string, string> NotificationTemplates { get; set; } = new();
    }

    /// <summary>
    /// 批量操作日志
    /// </summary>
    public class BatchOperationLog
    {
        /// <summary>
        /// 日志级别
        /// </summary>
        public BatchLogLevel Level { get; set; }

        /// <summary>
        /// 日志消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 详细信息
        /// </summary>
        public string Details { get; set; }

        /// <summary>
        /// 日志时间
        /// </summary>
        public DateTime LogTime { get; set; }

        /// <summary>
        /// 相关照片ID（如果适用）
        /// </summary>
        public Guid? PhotoId { get; set; }

        /// <summary>
        /// 相关子操作ID（如果适用）
        /// </summary>
        public Guid? SubOperationId { get; set; }

        /// <summary>
        /// 异常信息
        /// </summary>
        public string Exception { get; set; }

        /// <summary>
        /// 额外数据
        /// </summary>
        public Dictionary<string, object> AdditionalData { get; set; } = new();
    }

    /// <summary>
    /// 批量日志级别枚举
    /// </summary>
    public enum BatchLogLevel
    {
        /// <summary>
        /// 调试
        /// </summary>
        Debug = 0,

        /// <summary>
        /// 信息
        /// </summary>
        Info = 1,

        /// <summary>
        /// 警告
        /// </summary>
        Warning = 2,

        /// <summary>
        /// 错误
        /// </summary>
        Error = 3,

        /// <summary>
        /// 致命错误
        /// </summary>
        Fatal = 4
    }

    /// <summary>
    /// 照片批量子操作
    /// </summary>
    public class PhotoBatchSubOperation : FullAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; protected set; }

        /// <summary>
        /// 父操作ID
        /// </summary>
        public virtual Guid ParentOperationId { get; protected set; }

        /// <summary>
        /// 照片ID
        /// </summary>
        public virtual Guid PhotoId { get; protected set; }

        /// <summary>
        /// 子操作类型
        /// </summary>
        public virtual BatchOperationType OperationType { get; protected set; }

        /// <summary>
        /// 子操作状态
        /// </summary>
        public virtual BatchOperationStatus Status { get; protected set; }

        /// <summary>
        /// 操作参数
        /// </summary>
        public virtual Dictionary<string, object> Parameters { get; protected set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public virtual DateTime? StartTime { get; protected set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public virtual DateTime? CompletionTime { get; protected set; }

        /// <summary>
        /// 执行耗时（毫秒）
        /// </summary>
        public virtual long Duration { get; protected set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public virtual int RetryCount { get; protected set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public virtual string ErrorMessage { get; protected set; }

        /// <summary>
        /// 操作结果
        /// </summary>
        public virtual Dictionary<string, object> Result { get; protected set; }

        /// <summary>
        /// 父操作导航属性
        /// </summary>
        public virtual PhotoBatchOperation ParentOperation { get; protected set; }

        /// <summary>
        /// 照片导航属性
        /// </summary>
        public virtual Photo Photo { get; protected set; }

        protected PhotoBatchSubOperation()
        {
            Parameters = new Dictionary<string, object>();
            Result = new Dictionary<string, object>();
        }

        public PhotoBatchSubOperation(
            Guid id,
            Guid parentOperationId,
            Guid photoId,
            BatchOperationType operationType,
            Dictionary<string, object> parameters = null,
            Guid? tenantId = null) : base(id)
        {
            ParentOperationId = parentOperationId;
            PhotoId = photoId;
            OperationType = operationType;
            Parameters = parameters ?? new Dictionary<string, object>();
            TenantId = tenantId;
            Status = BatchOperationStatus.Pending;
        }

        /// <summary>
        /// 开始执行子操作
        /// </summary>
        public virtual void StartExecution()
        {
            Status = BatchOperationStatus.Running;
            StartTime = DateTime.Now;
        }

        /// <summary>
        /// 完成子操作
        /// </summary>
        public virtual void CompleteExecution(bool success, string errorMessage = null)
        {
            CompletionTime = DateTime.Now;
            if (StartTime.HasValue)
            {
                Duration = (long)(CompletionTime.Value - StartTime.Value).TotalMilliseconds;
            }

            if (success)
            {
                Status = BatchOperationStatus.Completed;
            }
            else
            {
                Status = BatchOperationStatus.Failed;
                ErrorMessage = errorMessage;
            }
        }

        /// <summary>
        /// 重试子操作
        /// </summary>
        public virtual void Retry()
        {
            RetryCount++;
            Status = BatchOperationStatus.Retrying;
            StartTime = DateTime.Now;
            ErrorMessage = null;
        }

        /// <summary>
        /// 更新操作结果
        /// </summary>
        public virtual void UpdateResult(Dictionary<string, object> result)
        {
            Result = result ?? new Dictionary<string, object>();
        }

        /// <summary>
        /// 设置操作参数
        /// </summary>
        public virtual void SetParameter(string key, object value)
        {
            Parameters[key] = value;
        }
    }
}